package ch.nostromo.tiffanys.clients.desktop.gui.settings;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;

import ch.nostromo.lib.swing.NosButton;
import ch.nostromo.lib.swing.NosLabel;
import ch.nostromo.lib.swing.NosPanel;
import ch.nostromo.lib.swing.NosRadioButton;
import ch.nostromo.lib.swing.NosTextField;
import ch.nostromo.lib.swing.text.NosTextFieldFilter;
import ch.nostromo.tiffanys.engines.EngineSettings;

public class EngineSettingsPanel extends NosPanel {

  private static final long serialVersionUID = 1L;
  private NosRadioButton btnFixedTimeMove;
  private NosTextField txtFixedDepth;
  private NosLabel lblMoves;
  private NosLabel nosLabel1;
  private NosLabel lblSeconds1;
  private NosLabel lblFixedDepth;
  private NosLabel lblFixedTimePerMove;
  private NosLabel lblFixedTimePerGame;
  private NosTextField txtFixedTimePerMove;
  private NosTextField txtFixedTimeMoveCount;
  private NosTextField txtFixedTimePerMoves;
  private NosButton btnOk;
  private NosButton btnCancel;
  private ButtonGroup gameModeGroup;
  private NosLabel lblGameMode;
  private NosRadioButton btnFixedTimeGame;
  private NosRadioButton btnFixedDepth;

  private EngineSettings engineSettings = null;

  public EngineSettingsPanel(EngineSettings engineSettings) {

      initGUI();
      initNOS(engineSettings);

  }

  private void initGUI() {

      {
        this.setPreferredSize(new java.awt.Dimension(473, 263));
        this.setLayout(null);
        gameModeGroup = new ButtonGroup();
        {
          btnFixedDepth = new NosRadioButton();
          this.add(btnFixedDepth);
          btnFixedDepth.setText("Fixed depth");
          btnFixedDepth.setBounds(52, 32, 253, 19);
          gameModeGroup.add(btnFixedDepth);
          btnFixedDepth.setTextTranslationKey("EngineSettings.FixedDepth");
        }
        {
          btnFixedTimeMove = new NosRadioButton();
          this.add(btnFixedTimeMove);
          btnFixedTimeMove.setText("Fixed time per move");
          btnFixedTimeMove.setBounds(52, 51, 253, 19);
          gameModeGroup.add(btnFixedTimeMove);
          btnFixedTimeMove.setTextTranslationKey("EngineSettings.FixedTimePerMove");
        }
        {
          btnFixedTimeGame = new NosRadioButton();
          this.add(btnFixedTimeGame);
          btnFixedTimeGame.setText("Fixed time per game");
          btnFixedTimeGame.setBounds(52, 70, 248, 19);
          gameModeGroup.add(btnFixedTimeGame);
          btnFixedTimeGame.setTextTranslationKey("EngineSettings.FixedTimePerGame");
        }
        {
          lblGameMode = new NosLabel();
          this.add(lblGameMode);
          {
            btnCancel = new NosButton();
            this.add(btnCancel);
            btnCancel.setText("Cancel");
            btnCancel.setBounds(290, 217, 130, 22);
            btnCancel.setTextTranslationKey("Common.Cancel");
            btnCancel.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                cancelAction();
              }
            });
          }
          {
            btnOk = new NosButton();
            this.add(btnOk);
            btnOk.setText("Ok");
            btnOk.setBounds(38, 217, 130, 22);
            btnOk.setTextTranslationKey("Common.Save");
            btnOk.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                okAction();
              }
            });
          }
          {
            lblFixedTimePerGame = new NosLabel();
            this.add(lblFixedTimePerGame);
            lblFixedTimePerGame.setText("Fixed time per x moves");
            lblFixedTimePerGame.setBounds(38, 171, 171, 15);
            lblFixedTimePerGame.setTextTranslationKey("EngineSettings.FixedTimePerGame");
          }
          {
            lblFixedTimePerMove = new NosLabel();
            this.add(lblFixedTimePerMove);
            lblFixedTimePerMove.setText("Fixed time per move");
            lblFixedTimePerMove.setBounds(38, 142, 171, 15);
            lblFixedTimePerMove.setTextTranslationKey("EngineSettings.FixedTimePerMove");
          }
          {
            lblFixedDepth = new NosLabel();
            this.add(lblFixedDepth);
            lblFixedDepth.setText("Fixed depth");
            lblFixedDepth.setBounds(38, 113, 158, 15);
            lblFixedDepth.setTextTranslationKey("EngineSettings.FixedDepth");
          }
          {
            lblSeconds1 = new NosLabel();
            this.add(lblSeconds1);
            lblSeconds1.setText("Sec.");
            lblSeconds1.setBounds(282, 142, 48, 15);
            lblSeconds1.setTextTranslationKey("EngineSettings.Sec");
          }
          {
            nosLabel1 = new NosLabel();
            this.add(nosLabel1);
            nosLabel1.setText("Sec.");
            nosLabel1.setBounds(282, 171, 42, 15);
            nosLabel1.setTextTranslationKey("EngineSettings.Sec");
          }
          {
            lblMoves = new NosLabel();
            this.add(lblMoves);
            lblMoves.setText("Moves");
            lblMoves.setBounds(384, 171, 69, 15);
            lblMoves.setTextTranslationKey("EngineSettings.Moves");
          }
          {
            txtFixedDepth = new NosTextField();
            this.add(txtFixedDepth);
            txtFixedDepth.setBounds(215, 110, 55, 22);
          }
          {
            txtFixedTimePerMove = new NosTextField();
            this.add(txtFixedTimePerMove);
            txtFixedTimePerMove.setBounds(215, 139, 55, 22);
          }
          {
            txtFixedTimePerMoves = new NosTextField();
            this.add(txtFixedTimePerMoves);
            txtFixedTimePerMoves.setBounds(215, 167, 55, 24);
          }
          {
            txtFixedTimeMoveCount = new NosTextField();
            this.add(txtFixedTimeMoveCount);
            txtFixedTimeMoveCount.setBounds(324, 168, 48, 22);
          }
          lblGameMode.setText("Game Mode");
          lblGameMode.setBounds(38, 11, 220, 15);
          lblGameMode.setTextTranslationKey("EngineSettings.GameMode");
        }
      }
  }

  private void initNOS(EngineSettings engineSettings)  {
    this.engineSettings = engineSettings;

    NosTextFieldFilter intFilter1 = new NosTextFieldFilter();
    intFilter1.setAcceptedCharacters("0123456789");
    NosTextFieldFilter intFilter2 = new NosTextFieldFilter();
    intFilter2.setAcceptedCharacters("0123456789");
    NosTextFieldFilter intFilter3 = new NosTextFieldFilter();
    intFilter3.setAcceptedCharacters("0123456789");
    NosTextFieldFilter intFilter4 = new NosTextFieldFilter();
    intFilter4.setAcceptedCharacters("0123456789");


    this.txtFixedDepth.setDocument(intFilter1);
    this.txtFixedTimeMoveCount.setDocument(intFilter2);
    this.txtFixedTimePerMove.setDocument(intFilter3);
    this.txtFixedTimePerMoves.setDocument(intFilter4);

    if (engineSettings.GAME_MODE == EngineSettings.GAME_MODE_FIXED_DEPTH) {
      this.btnFixedDepth.setSelected(true);
    } else if (engineSettings.GAME_MODE == EngineSettings.GAME_MODE_FIXED_TIME_PER_MOVE) {
      this.btnFixedTimeMove.setSelected(true);
    } else if (engineSettings.GAME_MODE == EngineSettings.GAME_MODE_FIXED_TIME_PER_MOVES) {
      this.btnFixedTimeGame.setSelected(true);
    }

    this.txtFixedDepth.setText(String.valueOf(engineSettings.FIXED_DEPTH));
    this.txtFixedTimePerMove.setText(String.valueOf(engineSettings.FIXED_TIME_PER_MOVE));
    this.txtFixedTimePerMoves.setText(String.valueOf(engineSettings.FIXED_TIME_PER_MOVES_SECONDS));
    this.txtFixedTimeMoveCount.setText(String.valueOf(engineSettings.FIXED_TIME_PER_MOVES_MOVE_COUNT));
  }

  void okAction() {
    if (btnFixedDepth.isSelected()) {
      engineSettings.GAME_MODE = EngineSettings.GAME_MODE_FIXED_DEPTH;
    } else if (btnFixedTimeMove.isSelected()) {
      engineSettings.GAME_MODE = EngineSettings.GAME_MODE_FIXED_TIME_PER_MOVE;
    } else if (btnFixedTimeGame.isSelected()) {
      engineSettings.GAME_MODE = EngineSettings.GAME_MODE_FIXED_TIME_PER_MOVES;
    }

    engineSettings.FIXED_DEPTH = Integer.valueOf(txtFixedDepth.getText()).intValue();
    engineSettings.FIXED_TIME_PER_MOVE = Integer.valueOf(txtFixedTimePerMove.getText()).intValue();
    engineSettings.FIXED_TIME_PER_MOVES_SECONDS = Integer.valueOf(txtFixedTimePerMoves.getText()).intValue();
    engineSettings.FIXED_TIME_PER_MOVES_MOVE_COUNT = Integer.valueOf(txtFixedTimeMoveCount.getText()).intValue();
    this.nosClose();
  }

  void cancelAction() {
    this.nosClose();
  }

}
